package com.kanas.webak.config.domain;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.codehaus.jackson.annotate.JsonIgnoreProperties;

import com.kanas.framework.support.AbstractModel;
import com.kanas.framework.util.StringUtils;

@Entity(name="KanasSequence")
@Table(name="kanas_sequence")
@JsonIgnoreProperties({""})
public class Sequence extends AbstractModel {

	@Transient
	private static final long serialVersionUID = 1L;

	public enum ResetIntervalType{
		DAY("d"),
		MONTH("m"),
		YEAR("y");
		private String code;
		ResetIntervalType(String code){
			this.code=code;
		}
		public String toString(){
			return code;
		}
	}
	
	@Id
	@Column(name="id", length=32)
	private String id;
	@Column(name="code", length=64, unique=true)
	private String code;
	@Column(name="label", length=200)
	private String label;
	
	//the current value(already used) of this sequence 
	@Column(name="current_value")
	private long value;
	@Column(name="reset_interval")
	private String resetInterval;
	@Column(name="last_update")
	@Temporal(TemporalType.TIMESTAMP)
	private Date lastUpdate;
	
	public Sequence() {
		this.id = StringUtils.generateUUID();
		reset();
	}
	public void reset(){
		this.value=0;
		this.lastUpdate=new Date();
	}
	
	public long increase(){
		this.value++;
		this.lastUpdate=new Date();
		return value;
	}
	
	@Override
	public String toIdString() {
		return id;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getLabel() {
		return label;
	}

	public void setLabel(String label) {
		this.label = label;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public long getValue() {
		return value;
	}

	public void setValue(long value) {
		this.value = value;
	}

	public String getResetInterval() {
		return resetInterval;
	}

	public void setResetInterval(String resetInterval) {
		this.resetInterval=(
			null==resetInterval?null:(
				"".equals(resetInterval.trim())?null:(
					"d".equalsIgnoreCase(resetInterval.trim())?"d":(//"d" for day
						"m".equalsIgnoreCase(resetInterval.trim())?"m":(//"m" for month
							"y".equalsIgnoreCase(resetInterval.trim())?"y":null//"y" for year
						)
					)
				)
			)
		);
	}

	public Date getLastUpdate() {
		return lastUpdate;
	}

	public void setLastUpdate(Date lastUpdate) {
		this.lastUpdate = lastUpdate;
	}

	
}
